package com.jiyun.service.impl;

import com.jiyun.pojo.Commodity;
import com.jiyun.mapper.CommodityMapper;
import com.jiyun.pojo.ShoppingOrder;
import com.jiyun.pojo.Result;
import com.jiyun.service.ICommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-17
 */
@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements ICommodityService {

    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<Commodity> commodityshowlist(Integer[] id) {
        // 创建一个集合用来存储查询结果
        List<Commodity> orders = new ArrayList<>();

        // 遍历传入的ID数组，逐个查询
        for (Integer ids : id) {
            // 假设你有一个查询方法按ID获取订单
            Commodity order = commodityMapper.commodityshowlist(ids);
            if (order != null) {
                orders.add(order);
            }
        }
        // 返回查询到的订单集合
        return orders;
    }





    @Override
    public List<Commodity> commodityshow(Commodity commodity) {
        return commodityMapper.commodityshow(commodity);
    }

    @Override
    public R add(Commodity commodity) {
        if(commodity.getId()==null){
            String url = (String) redisTemplate.opsForValue().get("fileUrl");
            commodity.setPhoto(url);
            commodity.setImages(url);
            commodity.setHid(0);
            commodityMapper.insert(commodity);
        }else {
            commodityMapper.updateById(commodity);
        }
        return R.ok();
    }

    @Override
    public R delete(Integer id) {
        commodityMapper.deleteById(id);
        return R.ok();
    }
}
